package com.xbongbong.pro.enums;

import com.xbongbong.i18n.util.I18nMessageUtil;
import lombok.Getter;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @author zhouwq
 * @date 2020/4/10 16:02
 */
@Getter
public enum  IntervalTypeEnum {


    /**
     * 年
     */
    YEAR("y", "enum.intervalType.describe.year", "yyyy","toStartOfYear","%Y-01-01","year"),

    /**
     * 年-季度
     */
    YEAR_QUARTER("y-q", "enum.intervalType.describe.year_quarter", "y-q","toStartOfQuarter","","quarter"),

    /**
     * 年-月
     */
    YEAR_MONTH("y-M", "enum.intervalType.describe.year_month", "yyyy-MM","toStartOfMonth","%Y-%m-01","month"),

    /**
     * 年-周
     */
    YEAR_WEEK("y-W", "enum.intervalType.describe.year_week", "y-W","toStartOfWeek","","week"),

    /**
     * 年-月-日
     */
    YEAR_MONTH_DAY("y-M-d", "enum.intervalType.describe.year_month_day", "yyyy-MM-dd","toStartOfDay","%Y-%m-%d","day");

    private String format;

    private String describe;

    private String dateFormat;
    /**
     * 日期提取函数
     */
    private String dateFunctions;
    /**
     * formatDateTime 对应的格式化参数
     */
    private String formatPlaceholder;
    /**
     * date_trunc 对应的格式化参数
     */
    private String dateTruncFmt;

    IntervalTypeEnum(String format, String describe, String dateFormat) {
        this.format = format;
        this.describe = describe;
        this.dateFormat = dateFormat;
    }
    IntervalTypeEnum(String format, String describe, String dateFormat,String dateFunctions) {
        this.format = format;
        this.describe = describe;
        this.dateFormat = dateFormat;
        this.dateFunctions = dateFunctions;
    }
    
    IntervalTypeEnum(String format, String describe, String dateFormat,String dateFunctions,String formatPlaceholder) {
        this.format = format;
        this.describe = describe;
        this.dateFormat = dateFormat;
        this.dateFunctions = dateFunctions;
        this.formatPlaceholder = formatPlaceholder;
    }

    IntervalTypeEnum(String format, String describe, String dateFormat, String dateFunctions, String formatPlaceholder, String dateTruncFmt) {
        this.format = format;
        this.describe = describe;
        this.dateFormat = dateFormat;
        this.dateFunctions = dateFunctions;
        this.formatPlaceholder = formatPlaceholder;
        this.dateTruncFmt = dateTruncFmt;
    }

    /**
     * 根据格式化信息返回枚举
     * @param format 格式化信息
     * @return 枚举类型
     */
    public static IntervalTypeEnum getByFormatStr(String format) {
        for (IntervalTypeEnum intervalTypeEnum : values()) {
            if (Objects.equals(format, intervalTypeEnum.getFormat())) {
                return intervalTypeEnum;
            }
        }
        return YEAR_MONTH_DAY;
    }
    public String getDescribe() {
        return I18nMessageUtil.getMessage(describe);
    }

    public String getI18nDescribe() {
        return describe;
    }

    /**
     * 钻取功能需要对时间字段拆分成5个，防止新扩展枚举逻辑没支持出错，所以不用values()
     * @return java.util.List<com.xbongbong.pro.enums.IntervalTypeEnum>
     * @throws
     * @author zcp
     * @since v1.0
     * @version v1.0
     */
    public static List<IntervalTypeEnum> drillParseList(){
        return Arrays.asList(YEAR, YEAR_QUARTER, YEAR_MONTH, YEAR_WEEK, YEAR_MONTH_DAY);
    }
}
